package junit;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;

/**
 * 继承BlockJUnit4ClassRunner，按照order的大小执行的，小的先执行。如order为1的，比order为2的先执行。
 * 
 * @author Beanstao@gmail.com
 * @date 2013-09-23 11:30
 * @version 1.0
 * 
 */
public class OrderedRunner extends BlockJUnit4ClassRunner
{
	public OrderedRunner(Class<?> klass) throws InitializationError
	{
		super(klass);
	}

	@Override
	protected List<FrameworkMethod> computeTestMethods()
	{
		List<FrameworkMethod> list = super.computeTestMethods();
		Collections.sort(list, new Comparator<FrameworkMethod>()
		{
			@Override
			public int compare(FrameworkMethod f1, FrameworkMethod f2)
			{
				Order o1 = f1.getAnnotation(Order.class);
				Order o2 = f2.getAnnotation(Order.class);
				if (o1 == null || o2 == null)
					return -1;
				return o1.order() - o2.order();
			}
		});
		return list;
	}
}
